package Java7_Concurrency_Cookbook.concurrency_test.phaser_test;

import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;

/**
 * Phaser.getPhase(): 返回phaser对象的当前阶段<br/>
 * Phaser.getRegisteredParties(): 返回使用phaser对象作为同步机制的任务数<br/>
 * Phaser.getArrivedParties(): 返回在一个阶段结束时, 已经到达的任务数<br/>
 * Phaser.getUnarrivedParties(): 返回在一个阶段结束时, 未到达的任务数<br/>
 * @author lixiangke
 * @date 2017/12/4
 */
public class Main {
    public static void main(String[] args) throws InterruptedException {
        Phaser phaser = new Phaser(3);
        for (int i = 0; i < 3; i++) {
            Task task = new Task(i + 1, phaser);
            Thread thread = new Thread(task);
            thread.start();
        }

        for (int i = 0; i < 10; i++) {
            System.out.printf("*****************\n");
            System.out.printf("Main: Phaser Log\n");
            System.out.printf("Main: Phaser: phase: %d\n", phaser.getPhase());
            System.out.printf("Main: Phaser: Registered Parties: %d\n", phaser.getRegisteredParties());
            System.out.printf("Main: Phaser: Arrived Parties: %d\n", phaser.getArrivedParties());
            System.out.printf("Main: Phaser: Unarrived Parties: %d\n", phaser.getUnarrivedParties());
            System.out.printf("*****************\n");
            TimeUnit.SECONDS.sleep(1);
        }
    }
}
